/*************************************************/
#include "stm32f10x.h"
#include "wf183de.h"
#include "public.h"

#include "SEGGER_RTT.h"

#define WF183DE_TIME_OUT 1000 // ???????1000??13us*1000 = 13ms

#define WF183DE_PORT GPIOC
#define WF183DE_PIN GPIO_Pin_8
#define WF183DE_PORT_RCC RCC_APB2Periph_GPIOC

#define WF183DE_DQ_OUT_H GPIO_SetBits(WF183DE_PORT, WF183DE_PIN)
#define WF183DE_DQ_OUT_L GPIO_ResetBits(WF183DE_PORT, WF183DE_PIN)

#define WF183DE_DQ_IN GPIO_ReadInputDataBit(WF183DE_PORT, WF183DE_PIN)

static uint8_t f_hl = 0;			//  ??????????
static uint8_t f_get_data_bits = 0; //  ???????????
static uint8_t f_rx = 0;			// ????????
static uint8_t f_rx_ok = 0;			// ??????????

static uint8_t r_h_delay = 0;  // ???????
static uint8_t r_l_delay = 0;  // ???????
static uint8_t bit_count = 0;  // ???
static uint8_t data_buf = 0;   // ?????
static uint8_t par_h = 0;	   // ???
static uint8_t par_l = 0;	   // ???
static uint8_t check_data = 0; // ????

static uint16_t par = 0; // ?????
/*****************************????****************************************************/
void wf183de_io_in(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Pin = WF183DE_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // ?????????
	GPIO_Init(WF183DE_PORT, &GPIO_InitStructure);
}
/*****************************????**************************************************/
void wf183de_io_out(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Pin = WF183DE_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // ???????????
	GPIO_Init(WF183DE_PORT, &GPIO_InitStructure);
}
/*****************************??????***************************************************/
void rx_start(void)
{
	if (WF183DE_DQ_IN)
	{
		r_l_delay = 0;
		r_h_delay = 0;
		f_hl = 0;
		f_get_data_bits = 0;
		bit_count = 0;
	}
}
/******************************????**************************************************/
void rx_data(void)
{
	if (!WF183DE_DQ_IN) // ??????????
	{
		if (f_hl) // ????????
		{
			f_hl = 0;			 // ??????????
			f_get_data_bits = 1; // ?????????
		}
		else
		{
			r_l_delay++;		// ?????????
			if (r_l_delay > 16) //?????????????
			{
				rx_start();// ??????
			}
		}
	}
	else// ??????????
	{
		f_hl = 1;// ??????????
		r_h_delay++;// ?????????
		if (r_h_delay > 16)//?????????????
		{
			rx_start();// ??????
		}
	}

	if (f_get_data_bits)// ?????????
	{
		f_get_data_bits = 0;// ?????????
		data_buf <<= 1;// ???????
		if (r_l_delay < r_h_delay)// ??????????????
		{
			data_buf |= 0x01;// ???????????? 1
		}

		r_h_delay = 0;//  ?????????
		r_l_delay = 1;// ?????????? 1
		bit_count++;// ?????

		if (bit_count >= 10)// ????? 10 ??
		{
			if (bit_count == 10)
			{
				par_h = data_buf;// ?????
			}

			if (bit_count == 20)
			{
				par_l = data_buf;// ?????
			}

			if (bit_count == 30)
			{
				check_data = data_buf;// ??????
				rx_start();// ??????
				f_rx = 0;// ????????
			}
		}
	}
}
/************************************************************************/
uint16_t get_wf183de_par(void)
{
	uint32_t time_out = WF183DE_TIME_OUT; // ??????

	uint8_t check_ok = 0; // ??????

	f_rx = 1;// ????????

	wf183de_io_out();// ???????

	WF183DE_DQ_OUT_L; //??????????? 0.2ms
	delay_us(200);

	wf183de_io_in();// ???????

	while ((time_out--) && (f_rx != 0)) // ?????????????
	{
		rx_data();// ????????
		delay_us(25);
	}

	if (f_rx == 0)// ????????
	{
		uint8_t temp = par_h + par_l;//  ????????

		if ((temp & 0xff) == check_data)// ???????
		{
			check_ok = 1;// ????
			par = par_h;// ????
			par <<= 8; // ?? 8 ?
			par |= par_l;//  ?????
		}
	}

	SEGGER_RTT_printf(0, "par=%d %d %d\r\n", par, f_rx, check_ok); //

	return par; // ?????
}
/************************************************************************/
void wf183de_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(WF183DE_PORT_RCC, ENABLE);

	GPIO_InitStructure.GPIO_Pin = WF183DE_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(WF183DE_PORT, &GPIO_InitStructure);
}
/***************************END OF FILE***************************/
